package cn.uexpo.lhz.controller.admin;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.uexpo.lhz.biz.DictionaryBiz;
import cn.uexpo.lhz.biz.DictionaryItemBiz;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.common.oldUtils.StringDate;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.Dictionary;
import cn.uexpo.lhz.domain.DictionaryExample;
import cn.uexpo.lhz.domain.DictionaryItem;
import cn.uexpo.lhz.domain.DictionaryItemExample;

/**
 *
 * @ClassName:  DictController
 * @Description:TODO(字典)
 * @author: 李冬建
 * @date:   2020年6月4日 上午10:29:17
 * @Copyright:  1.0.0
 */
@RestController
@RequestMapping("/admin/dict")
public class DictController extends BaseController<Dictionary>{

    @Autowired
    protected DictionaryBiz dictionaryBiz;
    @Autowired
    protected DictionaryItemBiz dictionaryItemBiz;
	/**
	 * 字典列表
	 * @return
	 * @throws Exception
	 */

//	@PostMapping(value="/findPage")
//	public HttpResult findPage(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
//		PageResult result=findPage1(pageRequest);
//		return HttpResult.ok(result);
//	}


//	private PageResult findPage1(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
//
//		Object dictName = pageRequest.getParamValue("dictName");//字典名称
//		Object dictCode = pageRequest.getParamValue("dictCode");//字典编码
//		Object dictType = pageRequest.getParamValue("dictType");//字典类型
//		DictionaryExample example=new DictionaryExample();
//		String orderBy=" DICT.createTime ";//按照序号降序
//	    String sort=" desc ";//默认按照时间降序
//		example.setOrderByClause(orderBy+sort);
//		DictionaryExample.Criteria criteria=example.createCriteria();
//		if (StringUtils.isNotBlank((String)dictName)) {
//			criteria.andNameLike("%"+(String)dictName+"%");
//		}
//		if (StringUtils.isNotBlank((String)dictCode)) {
//			criteria.andDictcodeLike("%"+(String)dictCode+"%");
//		}
//		if (StringUtils.isNotBlank((String)dictType)) {
//			criteria.andTypeLike("%"+(String)dictType+"%");
//		}
//		PageResult result=dictionaryBiz.findPage(pageRequest,example);
//		return result;
//	}

	/**
     * 添加/修改 字典
     * @param record
     * @return
	 * @throws Exception
     */
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody Dictionary record) {
		String msg="";
    	long result=0;
    	if (record.getId()==null) {
//    		字典编号不允许重复
    		DictionaryExample example=new DictionaryExample();
    		DictionaryExample.Criteria citeria=example.createCriteria();
    		if (StringUtils.isNotBlank(record.getDictcode())) {
    			citeria.andDictcodeEqualTo(record.getDictcode());
    		}
    		List<Dictionary> records=dictionaryBiz.selectByExample(example);
    		if (records!=null&&records.size()>0) {
    			msg="字典编码重复!";
    		}else {
    			record.setCreatetime(StringDate.getNow());//插入当前记录时间
    			result=dictionaryBiz.insertSelective(record);
	    	    if(result==1) {
	        		msg="添加字典成功！";
	        	}else {
	        		msg="添加字典失败！";
	    		}
    		}
		}else {
//			修改字典编号，先更新字典条目
			Dictionary oldRecord=dictionaryBiz.selectByPrimaryKey(record.getId());
			String oldDictCode=oldRecord.getDictcode();
			String newDictCode=record.getDictcode();
			DictionaryItemExample example=new DictionaryItemExample();
			DictionaryItemExample.Criteria citeria=example.createCriteria();
			citeria.andDictcodeEqualTo(oldDictCode);
			List<DictionaryItem> items=dictionaryItemBiz.selectByExample(example);
			if (items!=null&&items.size()>0) {
				for (DictionaryItem dictionaryItem : items) {
					dictionaryItem.setDictcode(newDictCode);
					dictionaryItemBiz.updateByPrimaryKeySelective(dictionaryItem);
				}
			}
//			result=dictionaryBiz.update(record);
			record.setUpdatetime(StringDate.getNow());//更新为当前日期
			result=dictionaryBiz.updateByPrimaryKeySelective(record);
			if(result==1) {
	    		msg="修改字典成功！";
	    	}else {
	    		msg="修改字典失败！";
			}
		}

    	if(result==1) {
    		return HttpResult.ok(msg);
    	}else {
    		return HttpResult.error(msg);
		}
	}

    /**
     * 删除字典
     * @param
     * @return
     * @throws Exception
     */

    @RequestMapping(value="/delete",method=RequestMethod.POST)
    public HttpResult dictDelete(@RequestBody List<Dictionary> records) throws Exception {
		if (records!=null&&records.size()>0) {
			for (Dictionary dict : records) {
				Integer id=dict.getId();
				//先删除字典条目
		    	Dictionary record=dictionaryBiz.selectByPrimaryKey(id);
		    	String dictCode=record.getDictcode();
		    	DictionaryItemExample example=new DictionaryItemExample();
		    	DictionaryItemExample.Criteria criteria=example.createCriteria();
		    	criteria.andDictcodeEqualTo(dictCode);
		    	List<DictionaryItem> items=dictionaryItemBiz.selectByExample(example);
		    	if(items!=null&&items.size()>0) {
					for (DictionaryItem item : items) {
						dictionaryItemBiz.deleteEntity(item);
					}
		    	}
		    	//再删除该字典
				long result=dictionaryBiz.deleteDictionaryByDictCode(dictCode);
			}
		}

		return HttpResult.ok(1);
    }

}
